//
//  VerResultController.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/10/7.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "VerResultController.h"
#import "VerRecordCell.h"
#import "VerRecordTCell.h"
#import "BillViewController.h"

@interface VerResultController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,weak)UIImageView *iconImg;
@property (nonatomic,weak)UILabel *detailL;
@property (nonatomic,weak)UIButton *recordBtn;

@property (nonatomic,strong)NSArray *listArr;
@end

@implementation VerResultController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"核销结果";
    _listArr = [_listDic hw_safeObjectForKey:@"datalist"];
    [self setUI];
}
- (void)setUI{
    
    UIView *headerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 230)];
    headerV.backgroundColor = DarkColor;
    UIImageView *iconImg = [HWCommon hw_roundedRectWith:40 rect:CGRectMake(0, 0, 80, 80) byRoundingCorners:UIRectCornerAllCorners];
    [headerV addSubview:iconImg];
    _iconImg = iconImg;
    
    UILabel *detailL = [[UILabel alloc] init];
    
    [detailL setTextAlignment:NSTextAlignmentCenter];
    [headerV addSubview:detailL];
    _detailL = detailL;
    
    NSInteger successNum = [_listDic[@"success_num"] integerValue];
    if (_listCode > successNum) {
        iconImg.image = [UIImage imageNamed:@"wanning"];
        detailL.text = @"部分核销成功";
    }
    if (_listCode == successNum) {
        iconImg.image = [UIImage imageNamed:@"success"];
        detailL.text = @"核销成功";
    }
    if (successNum == 0) {
        iconImg.image = [UIImage imageNamed:@"false"];
        detailL.text = @"核销失败";
    }
    

    
    UIButton *recordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [recordBtn setTitle:@"查看核销记录" forState:UIControlStateNormal];
    [recordBtn setTitleColor:WYColor(96, 171, 48) forState:UIControlStateNormal];
    recordBtn.layer.cornerRadius = 3;
    [recordBtn addTarget:self action:@selector(RecordClick) forControlEvents:UIControlEventTouchUpInside];
    recordBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [recordBtn setBackgroundColor:WYColor(217, 219, 216)];
    [headerV addSubview:recordBtn];
    _recordBtn = recordBtn;
    
    UILabel *titleL = [[UILabel alloc] init];
    titleL.text = @"   核销信息";
    titleL.font = [UIFont systemFontOfSize:14];
    [headerV addSubview:titleL];
    
    self.tableView.tableHeaderView = headerV;
    [self.view addSubview:self.tableView];
    __weak __typeof(&*self)weakSelf = self;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(weakSelf.view);
    }];
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(headerV);
        make.top.equalTo(headerV.mas_top).offset(40);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    [detailL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(headerV);
        make.top.equalTo(iconImg.mas_bottom).offset(8);
    }];
    [recordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(headerV);
        make.top.equalTo(detailL.mas_bottom).offset(8);
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(headerV);
        make.height.equalTo(@30);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _listArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *typeDic = _listArr[indexPath.row];
    if ([[typeDic hw_safeObjectForKey:@"writeoff_status"] integerValue] == 1) {
        VerRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identify" forIndexPath:indexPath];
        cell.dic = _listArr[indexPath.row];
        return cell;
    }else{
        VerRecordTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VerRecordTCell" forIndexPath:indexPath];
        cell.dic = _listArr[indexPath.row];
        return cell;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *typeDic = _listArr[indexPath.row];
    if ([[typeDic hw_safeObjectForKey:@"writeoff_status"] integerValue] == 1) {
        return 85;
    }else{
        return 105;
    }
    return 85;
}


- (UITableView *)tableView{
    
    if (_tableView == nil) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.separatorColor = WYColor(238, 238, 238);
        tableView.tableFooterView = [[UIView alloc] init];
        [tableView registerNib:[UINib nibWithNibName:@"VerRecordCell" bundle:nil] forCellReuseIdentifier:@"identify"];
          [tableView registerNib:[UINib nibWithNibName:@"VerRecordTCell" bundle:nil] forCellReuseIdentifier:@"VerRecordTCell"];
        [self.view addSubview:tableView];
        _tableView = tableView;
    }
    return _tableView;
    
}

- (void)RecordClick{
    [self.view endEditing:YES];
    
    //    VerRecordController *recordVC = [[VerRecordController alloc] init];
    //    [self.navigationController pushViewController:recordVC animated:YES];
    
    BillViewController *billVC = [[BillViewController alloc] init];
    [self.navigationController pushViewController:billVC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
